/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package santiago.view.game;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import santiago.domain.Canal;
import santiago.domain.CardinalPoint;
import santiago.domain.Field;
import santiago.domain.Game;
import santiago.domain.Lot;
import santiago.domain.Node;
import santiago.ressources.utils.ImageManager;
import santiago.view.ResourceManager;

/**
 *
 * @author Florent
 */
public class PanelGameboard extends JPanel {

    private Game Game;
    private static final int CanalThickness = 12;
    private static final int FieldWidth = 92;
    private static final int FieldHeight = 92;
    private static final int FieldSpacing = 4;
    private static final int SpringWidth = 20;
    private static final int SpringHeight = 20;
    private static final int LotWidth = FieldWidth * 2 + FieldSpacing + CanalThickness;
    private static final int LotHeight = FieldHeight * 2 + FieldSpacing + CanalThickness;
    private static final int CanalLength = LotWidth + CanalThickness * 2;

    public PanelGameboard(Game Game) {
        setPreferredSize(new Dimension(810, 612));
        repaint(Game);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D Context = (Graphics2D) g;
        //draw gameboard
        Context.drawImage(ImageManager.Gameboard.getBufferedImage(), 0, 0, null);

        //Transform gameboard upon game content
        Node[] Nodes = Game.getNodes();
        for (int i = 0; i < Nodes.length; i++) {
            //Compute upper right corner coordinates of the current node
            int x = CanalThickness + (i % 5) * LotWidth;
            int y = CanalThickness + ((int) (i / 5)) * LotHeight;

            //draw fields
            Lot Lot = Nodes[i].getLot(CardinalPoint.SOUTH_EAST);
            if (Lot != null) {
                drawField(Context, Lot.getField(CardinalPoint.NORTH_WEST), x, y);
                drawField(Context, Lot.getField(CardinalPoint.NORTH_EAST), x + FieldWidth + FieldSpacing, y);
                drawField(Context, Lot.getField(CardinalPoint.SOUTH_WEST), x, y + FieldHeight + FieldSpacing);
                drawField(Context, Lot.getField(CardinalPoint.SOUTH_EAST), x + FieldWidth + FieldSpacing, y + FieldHeight + FieldSpacing);
            }

            //draw canals
            Canal Canal = Nodes[i].getCanal(CardinalPoint.EAST);
            if (Canal != null && Canal.getContentColor() != null) {
                Context.setColor(Canal.getPrecinctColor());
                Context.drawRect(x, y, CanalLength, CanalThickness);
                Context.setColor(Canal.getContentColor());
                Context.drawRect(x + 1, y + 1, CanalLength - 2, CanalThickness - 2);
            }
            Canal = Nodes[i].getCanal(CardinalPoint.SOUTH);
            if (Canal != null && Canal.getContentColor() != null) {
                Context.setColor(Canal.getPrecinctColor());
                Context.drawRect(x, y, CanalThickness, CanalLength);
                Context.setColor(Canal.getContentColor());
                Context.drawRect(x + 1, y + 1, CanalThickness - 2, CanalLength - 2);
            }
        }

        //draw spring
        Context.drawImage(ImageManager.Spring.getBufferedImage(), (CanalThickness / 2 + (Game.getSpring() % 5) * LotWidth) - SpringWidth / 2, (CanalThickness / 2 + ((int) (Game.getSpring() / 5)) * LotHeight) - SpringHeight / 2, null);
    }

    /**
     * Draws a field depending on it's content. The drawing is done to the given
     * coordinates using the given graphics
     *
     * @param Context graphics on which to print the field content
     * @param Field a gameboard field that contains the game data (whom field is
     * it, is it a desert, has it a palmtree)
     * @param x
     * @param y
     */
    private void drawField(Graphics2D Context, Field Field, int x, int y) {
        if (Field.hasPlantation()) {
            Context.drawImage(ResourceManager.getPlantationImage(Field.getPlantation()), x, y, null);
        }
        if (Field.hasPlamTree()) {
            Context.drawImage(ImageManager.Palmtree.getBufferedImage(), x, y, null);
        }
        if (Field.isDesert()) {
            Context.drawImage(ImageManager.Desert.getBufferedImage(), x, y, this);
        }
    }

    public void repaint(Game Game) {
        this.Game = Game;
        repaint();
    }
}
